﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 子弹类型
/// </summary>
public enum BulletTypeEnum
{
	CollisionDisappear,				//碰撞消失
	Always,							//一直存在
}

/// <summary>
/// Bullet move type enum.
/// </summary>
public enum BulletMoveTypeEnum
{
	Line,							//直线运动
	Slash,							//斜线运动
	Follow,							//追踪运动
}

/// <summary>
/// Bullet 所属阵营
/// </summary>
public enum BulletCampEnum
{
	HeroCamp,						//英雄阵营
	MonCamp,						//怪物阵营
}

public class TBullet : TSprite {

	[HideInInspector][SerializeField] int id;								//id
//	[HideInInspector][SerializeField] int attack;							//攻击
	[HideInInspector][SerializeField] float moveSpeed;						//移动速度
	[HideInInspector][SerializeField] BulletTypeEnum bulletType;			//子弹类型
	[HideInInspector][SerializeField] BulletMoveTypeEnum bulletMoveType;	//子弹运动类型
	[HideInInspector][SerializeField] BulletCampEnum camp;					//子弹阵营
	
	EffectObj effectObj;
	int index;		//当前子弹的编号
	int level;		//创造此子弹的精灵的子弹等级
	float angle;	//子弹的偏移角度
	
	
	public void ResetBulletPos(Vector3 pos, int index, int level)
	{
		this.level = level;
		this.index = index;
		transform.position = pos;
		
		CalculatingPosDir();
		
		ChangeSpriteState(SpriteStateEnum.Move);
	}
	
	/// <summary>
	/// 计算位置和方向
	/// </summary>
	void CalculatingPosDir()
	{
		switch(bulletMoveType)
		{
		case BulletMoveTypeEnum.Line:
			CalculatingLine();
			break;
		case BulletMoveTypeEnum.Slash:
			CalculatingSlash();
			break;
		case BulletMoveTypeEnum.Follow:
			break;
		}
	}
	
	/// <summary>
	/// 计算Line型
	/// </summary>
	void CalculatingLine()
	{
		switch((BulletLevelEnum)level)
		{
		case BulletLevelEnum.One:
			break;
		case BulletLevelEnum.Two:
			if(index == (int)BulletLevelEnum.One)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_TWO, 0); 
			}
			else if(index == (int)BulletLevelEnum.Two)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_TWO, 1); 
			}
			break;
		case BulletLevelEnum.Three:
			if(index == (int)BulletLevelEnum.One)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_THREE, 0); 
			}
			else if(index == (int)BulletLevelEnum.Three)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_THREE, 1); 
			}
			break;
		case BulletLevelEnum.Four:
			if(index == (int)BulletLevelEnum.One)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_FOUR * 1.5f, 0); 
			}
			else if(index == (int)BulletLevelEnum.Two)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_FOUR * 0.5f, 0);
			}
			else if(index == (int)BulletLevelEnum.Three)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_FOUR * 0.5f, 1);
			}
			else if(index == (int)BulletLevelEnum.Four)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_FOUR * 1.5f, 1);
			}
			break;
		case BulletLevelEnum.Five:
			if(index == (int)BulletLevelEnum.One)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_FIVE * 2f, 0); 
			}
			else if(index == (int)BulletLevelEnum.Two)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_FIVE, 0);
			}
			else if(index == (int)BulletLevelEnum.Three)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_FIVE, 1);
			}
			else if(index == (int)BulletLevelEnum.Four)
			{
				OffsetPos(Constant.BULLET_LEVEL_OFFSET_FIVE * 2f, 1);
			}
			break;
		default:
			break;
		}
	}
	
	/// <summary>
	/// 计算 Slash 类型
	/// </summary>
	void CalculatingSlash()
	{
		angle = transform.localRotation.eulerAngles.z;
		switch((BulletLevelEnum)level)
		{
		case BulletLevelEnum.One:
			break;
		case BulletLevelEnum.Two:
			if(index == (int)BulletLevelEnum.One)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_TWO, 0); 
			}
			else if(index == (int)BulletLevelEnum.Two)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_TWO, 1); 
			}
			break;
		case BulletLevelEnum.Three:
			if(index == (int)BulletLevelEnum.One)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_THREE, 0); 
			}
			else if(index == (int)BulletLevelEnum.Three)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_THREE, 1); 
			}
			break;
		case BulletLevelEnum.Four:
			if(index == (int)BulletLevelEnum.One)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_FOUR * 1.5f, 0); 
			}
			else if(index == (int)BulletLevelEnum.Two)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_FOUR * 0.5f, 0);
			}
			else if(index == (int)BulletLevelEnum.Three)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_FOUR * 0.5f, 1);
			}
			else if(index == (int)BulletLevelEnum.Four)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_FOUR * 1.5f, 1);
			}
			break;
		case BulletLevelEnum.Five:
			if(index == (int)BulletLevelEnum.One)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_FIVE * 2f, 0); 
			}
			else if(index == (int)BulletLevelEnum.Two)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_FIVE, 0);
			}
			else if(index == (int)BulletLevelEnum.Three)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_FIVE, 1);
			}
			else if(index == (int)BulletLevelEnum.Four)
			{
				OffsetAngle(Constant.BULLET_LEVEL_ANGLE_FIVE * 2f, 1);
			}
			break;
		default:
			break;
		}
	}
	
	/// <summary>
	/// 偏移位置.
	/// </summary>
	void OffsetPos(Vector3 offset, int type)
	{
		if(type == 0)
		{
			transform.localPosition -= offset;
		}
		else if(type == 1)
		{
			transform.localPosition += offset;
		}
	}
	
	void OffsetAngle(float angle, int type)
	{
		var rotation = transform.localRotation.eulerAngles;
		if(type == 0)
		{
			rotation.z -= angle;
			transform.localRotation = Quaternion.Euler(rotation);
		}
		else if(type == 1)
		{
			rotation.z += angle;
			transform.localRotation = Quaternion.Euler(rotation);
		}
		
		this.angle = -rotation.z;
	}
	
	public override void ChangeMove ()
	{
//		base.ChangeMove ();		//子弹只需要改变状态，没有动画的改变
	}
	
	public override void Move ()
	{
//		base.Move ();
		
		switch(bulletMoveType)
		{
		case BulletMoveTypeEnum.Line:
			MoveLine();
			break;
		case BulletMoveTypeEnum.Slash:
			MoveSlash();
			break;
		case BulletMoveTypeEnum.Follow:
			MoveFollow();
			break;
		}
		
		if(viewRect != null && !viewRect.IsInViewRect(transform.localPosition))
		{
			BulletManager.SetFreeEffectObj(id.ToString(), effectObj);
		}
		
//		CheckCollider();
	}
	
	/// <summary>
	/// 直线运动
	/// </summary>
	void MoveLine()
	{	
		moveVector = transform.localPosition;
		moveVector.y += MoveSpeed;
		
		transform.localPosition = moveVector;
	}
	
	/// <summary>
	/// 斜线运动
	/// </summary>
	void MoveSlash()
	{
		moveVector = Vector3.zero;
		
		moveVector.x = Mathf.Sin(angle * Mathf.Deg2Rad) * MoveSpeed;
		moveVector.y = Mathf.Cos(angle * Mathf.Deg2Rad) * MoveSpeed;
		
		transform.localPosition += moveVector;
	}
	
	/// <summary>
	/// 跟随运动
	/// </summary>
	void MoveFollow()
	{
		moveVector = Vector3.zero;
		float angle = Vector2.Angle(transform.localPosition, target.localPosition) * Mathf.Deg2Rad;

		moveVector.x = Mathf.Sin(angle) * MoveSpeed;
		moveVector.y = Mathf.Cos(angle) * MoveSpeed;
		
		transform.localPosition += moveVector;
	}
	
	public override void OnSpriteLoadOverAtals ()
	{
		base.OnSpriteLoadOverAtals ();
		UISprite sprite = transform.GetComponent<UISprite>();
		sprite.MakePixelPerfect();
		
		BulletManager.SetFreeEffectObj(id.ToString(), effectObj);
	}
	
	public override void OnLastFrameCallBack ()
	{
//		base.OnLastFrameCallBack ();
		//目前子弹没有动画， 无需替换播放
	}
	
	/// <summary>
	/// 检测碰撞区域
	/// </summary>
	void CheckCollider()
	{
		if(Camp == BulletCampEnum.HeroCamp)
		{
			var list = ObjectManager.GetInstance().GetMonsterObjList();
			for(int i = 0; i < list.Count; i++)
			{
				var dis = Vector3.Distance(list[i].sprite.GetPos(), GetPos());
//				Debug.LogError("  dis  " + dis + "     " + list[i].sprite.GetPos() + "       " + GetPos());
				if(dis < Constant.CHECK_DIS)
				{
					list[i].sprite.ChangeSpriteState(SpriteStateEnum.Die);	//先设定为 碰到就死，后面再通过血量控制是否死亡
					ChangeSpriteState(SpriteStateEnum.Die);
					break;
				}
			}
		}
		else if(Camp == BulletCampEnum.MonCamp)
		{
			var list = Thero.GetInstance().GetHeroList();
			for(int i = 0; i < list.Count; i++)
			{
				if(list[i] != null && Vector3.Distance(list[i].GetPos(), GetPos()) < Constant.CHECK_DIS)
				{
					list[i].ChangeSpriteState(SpriteStateEnum.Die);
					ChangeSpriteState(SpriteStateEnum.Die);
					break;
				}
			}
		}
	}
	
	void OnTriggerEnter(Collider other) {
//		Destroy(other.gameObject);
		
		var sprite = other.gameObject.GetComponent<TSprite>();
		
		Debug.LogError(" sprite " + other.gameObject);
		if(sprite != null)
		{
			if(IsCanAttack(Camp, sprite))
			{
				sprite.ChangeSpriteState(SpriteStateEnum.Die);
				ChangeSpriteState(SpriteStateEnum.Die);
			}
			
		}
	}
	
	public override void Death ()
	{
//		base.Death ();
	
		Debug.LogError(" 子弹死亡 " + gameObject);
		BulletManager.SetFreeEffectObj(id.ToString(), effectObj);
	}
	
	public int Id
	{
		get{return id;}
		set{id = value;}
	}
	
//	public int Attack
//	{
//		get{return attack;}
//		set{attack = value;}
//	}
	
	public float MoveSpeed
	{
		get{return moveSpeed;}
		set{moveSpeed = value;}
	}
	
	public BulletTypeEnum BulletType
	{
		get{return bulletType;}
		set{bulletType = value;}
	}
	
	public BulletMoveTypeEnum BulletMoveType
	{
		get{return bulletMoveType;}
		set{bulletMoveType = value;}
	}
	
	public BulletCampEnum Camp
	{
		get{return camp;}
		set{camp = value;}
	}
	
	public EffectObj Obj
	{
		get{return effectObj;}
		set{effectObj = value;}
	}
}
